<template>
  <el-form :model="form" :rules="rules" ref="formRef">
    <el-form-item label="序号" prop="number" :label-width="formLabelWidth">
      <el-input v-model="form.number" style="width:50%"></el-input>
    </el-form-item>
    <el-form-item label="会员名称" prop="levelName" :label-width="formLabelWidth">
      <el-input type="text" v-model="form.levelName" style="width:50%"></el-input>
    </el-form-item>
    <el-form-item label="最小升级金额" prop="minMoney" :label-width="formLabelWidth">
      <el-input-number
        type="number"
        :min="0"
        :max="1000000000"
        controls-position="right"
        v-model="form.minMoney"
        :precision="0"
        :step="1"
      ></el-input-number>
      <span style="margin-left:10px">元</span>
    </el-form-item>
    <el-form-item label="最大升级金额" prop="maxMoney" :label-width="formLabelWidth">
      <el-input-number
        type="number"
        :min="0"
        :max="1000000000"
        controls-position="right"
        v-model="form.maxMoney"
        :precision="0"
        :step="1"
      ></el-input-number>
      <span style="margin-left:10px">元</span>
    </el-form-item>
    <el-form-item label="备注" prop="remark" :label-width="formLabelWidth">
      <el-input type="textarea" :rows="4" v-model="form.remark"></el-input>
    </el-form-item>
    <el-form-item :label-width="formLabelWidth">
      <el-button plain @click="resetForm('formRef')">取消</el-button>
      <el-button type="primary" @click="submitForm('formRef')">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      form: {
        number: "",
        levelName: "",
        minMoney: "",
        maxMoney: "",
        remark: ""
      },
      formLabelWidth: "120px",
      rules: {
        number: [
          { required: true, message: "请输入序号", trigger: "blur" },
        ],
        levelName: [
          { required: true, message: "请输入会员名称", trigger: "blur" },
          { max: 50, message: "长度不能超过50个字符", trigger: "blur" }
        ],
        minMoney: [
          { required: true, message: "请输入最小升级金额", trigger: "blur" },
        ],
        maxMoney: [
          { required: true, message: "请输入最大升级金额", trigger: "blur" },
        ],
        remark: [
          { required: true, message: "请输入备注", trigger: "blur" },
          { max: 50, message: "长度不能超过50个字符", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
            if(this.form.minMoney >this.form.maxMoney){
                this.$message.error("最小升级金额应小于最大升级金额");
                return;
            }
          this.$service.membershipLevelDicInsterLevel(this.form).then(res => {
            if (res.code == "00000") {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.$refs[formName].resetFields();
              this.$emit("submit", false);
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$emit("submit", false);
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss">
</style>